﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HaiFang
{
    public enum PageMode
    {
        GridMode,
        DetailMode
    }


    public interface IPageControl
    {
        int CurrentPage { get; set; }
        int TotalRows { get; set; }
        int PageSize { get; set; }
        event Action<int> OnCurrentPageChange;
    }

    public interface IStatusControl
    {
        event Action<string> OnCurrentStatusChange;
    }

    public class SampleGridTemplate : System.Web.UI.Page
    {

        override protected void OnInit(EventArgs e)
        {

            SetUserControl();

            this.InitControls();

            base.OnInit(e);
        }

        protected virtual void SetUserControl()
        {

        }

        protected virtual void OnCurrentModeChanged(PageMode PreviousMode, PageMode CurrentMode)
        {

        }

        protected int m_lTotalRowCount = 0;

        #region ISampleGridTemplate 成员

        public HiddenField KeyValue;

        public Panel TopPanel;

        public Panel GridPanel;

        public Panel DetailPanel;

        public Panel InformationPanel;

        public Label LabelInformation;

        public Button BtnSave;

        public Button BtnBack2Grid;

        public DataGrid ObjectGrid;

        public IButtonControl BtnCreate;

        public Label LabelCreateNew;

        protected int DeleteButtonCellIndex = 0;

        protected string DeleteButtonName = "btnDelete";

        public IPageControl PageControls = null;

        public IStatusControl StatusControls = null;

        protected string OrderBy = "";

        public PageMode CurrentMode
        {
            get
            {
                if (ViewState["CurrentMode"] == null)
                    ViewState["CurrentMode"] = PageMode.GridMode;

                return (PageMode)ViewState["CurrentMode"];
            }
            set
            {
                if (ViewState["CurrentMode"] == null || (PageMode)ViewState["CurrentMode"] != value)
                {
                    PageMode PreviousMode = (ViewState["CurrentMode"] == null ? PageMode.GridMode : (PageMode)ViewState["CurrentMode"]);

                    ViewState["CurrentMode"] = value;

                    if (((PageMode)ViewState["CurrentMode"]) == PageMode.DetailMode)
                    {
                        if (this.KeyValue.Value != "")
                        {
                            int id;
                            if (Int32.TryParse(this.KeyValue.Value,out id))
                            {
                                this.BindObject2DetailPanel(Convert.ToInt32(this.KeyValue.Value));
                            }
                            else
                            {
                                this.BindObject2DetailPanel(this.KeyValue.Value);
                            }
                        }
                        else
                        {
                            this.ClearDetailPanel();
                        }
                        this.LabelInformation.Text = "";
                        this.GridPanel.Visible = false;
                        this.DetailPanel.Visible = true;
                        this.InformationPanel.Visible = true;
                        //(this.BtnCreate as Control).Visible = false;
                        //this.LabelCreateNew.Visible = false;
                    }
                    else
                    {
                        if (this.PageControls != null)
                        {
                            this.GridBind(OrderBy, this.PageControls.CurrentPage, this.PageControls.PageSize, out m_lTotalRowCount);
                        }
                        this.GridPanel.Visible = true;
                        this.DetailPanel.Visible = false;
                        this.InformationPanel.Visible = false;
                        //(this.BtnCreate as Control).Visible = true;
                        //this.LabelCreateNew.Visible = true;
                    }

                    this.OnCurrentModeChanged(PreviousMode, (PageMode)ViewState["CurrentMode"]);

                }
            }
        }

        protected void RefreshGrid()
        {
            this.GridBind(OrderBy, this.PageControls.CurrentPage, this.PageControls.PageSize, out m_lTotalRowCount);
        }

        public virtual void BindObject2DetailPanel(int ID)
        {

        }

        public virtual void BindObject2DetailPanel(string Key)
        {

        }
        public virtual void ClearDetailPanel()
        {

        }

        public virtual void GridBind(string sOrderBy, int nPageJump, int nPageSize, out int lTotalRows)
        {
            lTotalRows = 0;
        }

        public virtual void Save(object ob)
        {

        }

        public virtual void Update(object ob)
        {

        }

        public virtual void Delete(object KeyObject)
        {

        }

        public virtual object GetValueFromDetailPanel()
        {
            return null;
        }

        protected virtual void DoOtherCommand(object CommangArgment)
        {

        }

        protected virtual void OnGridItemDataBound(object sender, DataGridItemEventArgs e)
        {

        }
        #endregion

        protected void InitControls()
        {
            if (BtnSave != null)
                this.BtnSave.Click += new EventHandler(BtnSave_Click);
            if (BtnBack2Grid != null)
                this.BtnBack2Grid.Click += new EventHandler(BtnBack2Grid_Click);
            if (BtnCreate != null)
                this.BtnCreate.Click += new EventHandler(BtnCreate_Click);
            //=new ImageClickEventHandler(BtnCreate_Click);

            if (this.ObjectGrid != null)
            {
                this.ObjectGrid.ItemCommand += new DataGridCommandEventHandler(ObjectGrid_ItemCommand);
                this.ObjectGrid.ItemDataBound += new DataGridItemEventHandler(ObjectGrid_ItemDataBound);
            }

            if (this.PageControls != null)
            {
                this.PageControls.OnCurrentPageChange += new Action<int>(PageControls_OnCurrentPageChange);
            }

            if (this.StatusControls != null)
            {
                this.StatusControls.OnCurrentStatusChange += new Action<string>(StatusControls_OnCurrentStatusChange);
            }

            if (this.TopPanel != null)
            {
                this.TopPanel.Visible = true;
            }

            if (this.GridPanel != null)
            {
                this.GridPanel.Visible = true;
            }

            if(this.DetailPanel!=null)
            {
                this.DetailPanel.Visible = false;
            }

            if (this.InformationPanel != null)
            {
                this.InformationPanel.Visible = false;
            }
        }

        void PageControls_OnCurrentPageChange(int obj)
        {

            this.GridBind(OrderBy, obj, this.PageControls.PageSize, out this.m_lTotalRowCount);

            this.PageControls.TotalRows = m_lTotalRowCount;
        }

        protected virtual void StatusControls_OnCurrentStatusChange(string obj)
        {

        }

        void ObjectGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            DataGrid grid = sender as DataGrid;
            Button btn = e.Item.Cells[this.DeleteButtonCellIndex].FindControl(this.DeleteButtonName) as Button;
            if (btn != null)
            {
                btn.Attributes["onclick"] = "javascript:return confirm('确定删除该数据吗？');";
            }
            if (e.Item.ItemIndex > -1)
            {
                e.Item.Attributes["onmouseout"] = "javascript:this.style.backgroundColor=''";
                e.Item.Attributes["onmouseover"] = "javascript:this.style.backgroundColor='#BED5F3';";
                e.Item.Attributes["onclick"] = string.Format("javascript:selectedrow('{0}',{1});", grid.ClientID, e.Item.ItemIndex);
            }

            OnGridItemDataBound(sender, e);
        }

        void ObjectGrid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Update":
                    this.KeyValue.Value = e.CommandArgument.ToString();
                    this.CurrentMode = PageMode.DetailMode;
                    break;
                case "Delete":
                    this.Delete(e.CommandArgument);
                    this.GridBind(OrderBy, this.PageControls.CurrentPage, this.PageControls.PageSize, out m_lTotalRowCount);
                    break;
                default:
                    DoOtherCommand(e);
                    break;
            }
        }


        protected virtual void BtnCreate_Click(object sender, EventArgs e)
        {
            
        }

        protected void BtnBack2Grid_Click(object sender, EventArgs e)
        {
            this.KeyValue.Value = "";
            this.CurrentMode = PageMode.GridMode;
        }

        void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                object ob = GetValueFromDetailPanel();

                if (this.KeyValue.Value != "")
                {
                    this.Update(ob);
                }
                else
                {
                    this.Save(ob);
                }

                this.LabelInformation.Text = "操作成功!";
            }
            catch (Exception ex)
            {
                this.LabelInformation.Text = "操作失败！" + ex.Message;
            }
        }

    }
}
